home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-21 | 3.7 KB | 118 lines | [TEXT/CWIE] |
- // ImageCopier.cp, use the declared copying objects (CopyGrafs, DynamicCopy, and
- // CopyBuffers) in your code to copy images instead of using CopyBits. Each
- // operator() guarantees to set the port and the addressing mode back to their
- // previous states when the function returns. Use CopyGrafs as you would CopyBits,
- // i.e. the port must be set to the destination first.
-
- // copyright © 1995, Macneil Shonle. All rights reserved.
-
- #ifndef __IMAGECOPIER__
- #include <ImageCopier.h>
- #endif
-
- #ifndef __GWORLDSETTER__
- #include <GWorldSetter.h>
- #endif
-
- CGrafPortCopier CopyGrafs;
- BufferCopier CopyBuffers;
-
- // sets the cache to an invalid state. Validate the cache by setting it to two legal ports,
- // the "use" member-function can do this
- CGrafPortCopier::CGrafPortCopier()
- : source(0), dest(0)
- {
- }
-
- // sets the cache to the parameters
- CGrafPortCopier::CGrafPortCopier(CGrafPtr srcPort, CGrafPtr dstPort)
- : source(srcPort), dest(dstPort)
- {
- }
-
- // copies the images within the given rectangles, using the cached source and destination
- // ports. Throws invalidargument if the cached source or destination port is invalid
- // may throw: invalidargument
- void CGrafPortCopier::operator()(const Rect& srcR, const Rect& dstR)
- throw(invalidargument) const
- {
- if (!source || !dest)
- throw invalidargument("source or dest not cached in", "GrafPortCopier::operator()");
-
- ::CopyBits(&GrafPtr(source)->portBits, &GrafPtr(dest)->portBits,
- &srcR, &dstR, srcCopy, nil);
- }
-
- // caches the ports to the parameters, and copies the images within the given rectangles
- void CGrafPortCopier::operator()(CGrafPtr src, CGrafPtr dest, const Rect& srcR,
- const Rect& destR)
- {
- this->use(src, dest);
- this->operator()(srcR, destR);
- }
-
- // caches the ports to the parameters
- void CGrafPortCopier::use(CGrafPtr srcPort, CGrafPtr dstPort)
- {
- source = srcPort;
- dest = dstPort;
- }
-
- // sets the cache to an invalid state. Validate the cache by setting it to two legal buffers,
- // the "use" member-function can do this
- BufferCopier::BufferCopier()
- : source(0), dest(0)
- {
- }
-
- // sets the cache to the parameters
- BufferCopier::BufferCopier(BufferAccessor& srcBuff, BufferAccessor& destBuff)
- : source(&srcBuff), dest(&destBuff)
- {
- }
-
- // copies the images within the given rectangles, using the cached source and destination
- // buffers. Throws invalidargument if the cached source or destination buffer is invalid
- // may throw: invalidargument
- void BufferCopier::operator()(const Rect& srcR, const Rect& dstR) throw(invalidargument) const
- {
- if (!source || !dest)
- throw invalidargument("source or dest not cached in", "BufferCopier::operator()");
-
- FourPixelsPtr srcPtr = (FourPixelsPtr)source->pixelAddr(srcR.left, srcR.top);
- FourPixelsPtr destPtr = (FourPixelsPtr)dest->pixelAddr(dstR.left, dstR.top);
- PixelCord numRows = srcR.bottom - srcR.top;
- PixelCord copyWidth = srcR.right - srcR.left;
- PixelCord copyWidthDiv4 = copyWidth / 4;
- RowBytes srcRowSkip = source->rowBytes() - copyWidth;
- RowBytes destRowSkip = dest->rowBytes() - copyWidth;
-
- do {
- for (PixelCord x=0; x<copyWidthDiv4; x++)
- *destPtr++ = *srcPtr++;
-
- if (copyWidth & 0x2)
- *TwoPixelsPtr(destPtr)++ = *TwoPixelsPtr(srcPtr)++;
-
- if (copyWidth & 0x1)
- *PixelPtr(destPtr)++ = *PixelPtr(srcPtr)++;
-
- PixelPtr(srcPtr) += srcRowSkip;
- PixelPtr(destPtr) += destRowSkip;
- } while (--numRows > 0);
- }
-
- // caches the buffers to the parameters, and copies the images within the given rectangles
- void BufferCopier::operator()(BufferAccessor& src, BufferAccessor& dest,
- const Rect& srcR, const Rect& destR)
- {
- this->use(src, dest);
- this->operator()(srcR, destR);
- }
-
- // caches the buffers to the parameters
- void BufferCopier::use(BufferAccessor& srcPort, BufferAccessor& dstPort)
- {
- source = &srcPort;
- dest = &dstPort;
- }